package MyIterator;

import MyObjectList.ItemList;

import java.util.List;

public class ItemIterator implements MyIterator {
    private ItemList itemList;
    private List items;
    private int current = 0;
    private int end = 0;

    public ItemIterator(ItemList itemList) {
        this.itemList = itemList;
        items = itemList.getItems();
        current = items.size() - 1;
        end = items.size() - 1;
    }

    @Override
    public void moveNext() {
        current++;
    }

    @Override
    public boolean isEnd() {
        return current == end;
    }

    @Override
    public void movePrevious() {
        if (!isStart()) {
            current--;
        }
    }

    @Override
    public boolean isStart() {
        return current == 0;
    }

    @Override
    public Object fetchNextItem() {
        int cursor = current;
        return items.get(++cursor);
    }

    @Override
    public Object fetchPreviousItem() {
        int cursor = current;
        return items.get(--cursor);
    }
}
